<?php
namespace app\admin\controller;
use app\admin\model\Base;
use think\Controller;
use think\Db;
use think\Model;
class Carousel extends Common{
    public function _initialize(){
        parent::_initialize();
    }
    //跳轮播管理页面-------
    public function show(){

        $result = new \app\admin\model\Carousel();
        $result = $result->index();
        $this->assign('carousel',$result);
        return $this->fetch('Carousel/show');

    }
    public function add(){
        //获取前台传过来的数据
        $sort=I('sort');
        $link=I('link');
        //调用父类的uploads
        $img=parent::uploads();
        //判断数据是否是空值
        if(!empty($sort)&&!empty($link)&&!empty($img)){
            $data = ['image' => $img, 'link' => $link , 'sort' => $sort,'delete'=>0];
            //使用 Db 类的 insert 方法向数据库提交数据
            $rs = Db::table('carousel')->insert($data);
            //判断是否添加成功，
            if($rs){
                $this->success("添加成功",url($this->controller.'/show'));
            }else{
                $this->error("添加失败");
            }
        }else{
            $this->error("表单不能为空");
        }

    }

    public function update(){
        //获取前台传过来的数据
        $id=I('id');
        $sort=I('sort');
        $link=I('link');
        //调用父类的uploads
        $img=parent::uploads();
        //判断数据是否是空值
        if(!empty($sort)&&!empty($link)&&!empty($img)){
            $data = ['image' => $img, 'link' => $link , 'sort' => $sort];
            //使用 Db 类的 insert 方法向数据库提交数据
            $rs = Db::table('carousel')->where('id='.$id)->update($data);
            //判断是否添加成功，
            if($rs){
                $this->success("修改成功",url($this->controller.'/index'));
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("表单不能为空");
        }
    }

    public function del(){
        $id=isset($_POST['id'])?$_POST['id']:"";
        $data = ['delete' => 1];
        $del=new Base();
        $del=$del->status('carousel',$id,$data);
        echo $del;
    }

}